Difference between std::bitset and std::vector<bool> and an array of bool

Vector of bool and array of bool can also be implemented to store a sequence of boolean values like bitset but there are some differences between each implementation: 

Parameter

bitset

vector of bool

array of bool

DefinitionA class template consisting of a sequence of bits stored such that each bit occupies 1 bit of memory.A variation of vectors of C++ STL in which each element is of size 1 bit and is of type boolA fixed size contiguous collection of bool data elements.
SizeFixed Size.Dynamic Size.Fixed Size.
MemoryA single element occupies 1 bit of memory.A single element occupies 1 bit of memory.A single element occupies 1 byte of memory.
SpeedSameSameFaster



C++ bitset and its application

A bitset is an array of bools but each boolean value is not stored in a separate byte instead, bitset optimizes the space such that each boolean value takes 1-bit space only, so space taken by bitset is less than that of an array of bool or vector of bool

A limitation of the bitset is that size must be known at compile time i.e. size of the bitset is fixed.

std::bitset is the class template for bitset that is defined inside <bitset> header file so we need to include the header file before using bitset in our program.

Syntax:

bitset<size> variable_name(initialization);

We can initialize bitset in three ways :

1. Uninitialized: All the bits will be set to zero.

bitset<size> variable_name;

2. Initialization with decimal integer: Bitset will represent the given decimal number in binary form.

bitset<size> variable_name(DECIMAL_NUMBER);

3. Initialization with binary string: Bitset will represent the given binary string.

bitset<size> variable_name(string("BINARY_STRING"));
bitset<size> variable_name("BINARY_STRING");

Example:

C++
// C++ program to demonstrate the bitset 
#include <bitset>
#include <iostream>

using namespace std;

int main()
{
    // declaring an uninitialized bitset object
    bitset<8> uninitializedBitset;

    // initialization with decimal number
    bitset<8> decimalBitset(15);

    // initialization with binary string
    bitset<8> stringBitset(string("1111"));

    cout << "Uninitialized bitset: " << uninitializedBitset
         << endl;
    cout << "Initialized with decimal: " << decimalBitset
         << endl;
    cout << "Initialized with string: " << stringBitset
         << endl;

    return 0;
}

Output
Uninitialized bitset: 00000000
Initialized with decimal: 00001111
Initialized with string: 00001111

Similar Reads

std::bitset Member Functions

std::bitset class contains some useful member functions to work on the bitset objects. Here is the list of some member functions of std::bitset:...

std::bitset Operators

Some of the basic operators are overloaded to work with bitset objects. Following is the list of those operators:...

Difference between std::bitset and std::vector and an array of bool

Vector of bool and array of bool can also be implemented to store a sequence of boolean values like bitset but there are some differences between each implementation:...

Contact Us